//
//  MyCommentNoImgCell.swift
//  maosheng
//
//  Created by lxl on 2019/8/13.
//

import UIKit

class MyCommentNoImgCell: UITableViewCell {

    let titleLab = UILabel()
    let deleteBtn = UIButton(type: .custom)
    let mainView = UIView()
    let userImg = UIImageView()
    let userLab = UILabel()
    let timeLab = UILabel()
    let commentLab = UILabel()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configCell()
        contentView.backgroundColor = KYP_f2f2f2_COLOR
        
    }
    
    
    func configCell() -> Void {
        mainView.backgroundColor = .white
        contentView.addSubview(mainView)
        mainView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        let imgHeight = 35
        userImg.layer.cornerRadius = CGFloat(imgHeight/2)
        userImg.layer.masksToBounds = true
        mainView.addSubview(userImg)
        userImg.snp.makeConstraints { (make) in
            make.top.left.equalTo(15)
            make.width.height.equalTo(imgHeight)
        }
        
        mainView.addSubview(userLab)
        userLab.textColor = KYP_333333_COLOR
        userLab.font = UIFont(name: PF_Regular, size: 12)
        userLab.snp.makeConstraints { (make) in
            make.left.equalTo(userImg.snp.right).offset(10)
            make.top.equalTo(18)
        }
        
        mainView.addSubview(timeLab)
        timeLab.textColor = KYP_999999_COLOR
        timeLab.font = UIFont(name: PF_Regular, size: 8)
        timeLab.snp.makeConstraints { (make) in
            make.left.equalTo(userLab)
            make.top.equalTo(userLab.snp.bottom).offset(5)
        }
        
        deleteBtn.setImage(UIImage(named: "comment_delete"), for: .normal)
        mainView.addSubview(deleteBtn)
        deleteBtn.snp.makeConstraints { (make) in
            make.top.equalTo(30)
            make.right.equalTo(-10)
            make.height.width.equalTo(35)
        }
        
        
        commentLab.numberOfLines = 0
        mainView.addSubview(commentLab)
        commentLab.textColor = KYP_333333_COLOR
        commentLab.font = UIFont(name: PF_Regular, size: 11)
        commentLab.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(userImg.snp.bottom).offset(13)
        }
        
        let lineView = UIView()
        lineView.backgroundColor = TABLE_LINE_COLOR
        mainView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(commentLab.snp.bottom).offset(11)
            make.height.equalTo(0.5)
        }
        
        
        
        mainView.addSubview(titleLab)
        titleLab.textAlignment = .left
        titleLab.numberOfLines = 0
        titleLab.lineBreakMode = .byWordWrapping
        titleLab.textColor = KYP_333333_COLOR
        titleLab.font = UIFont(name: PF_Medium, size: 15)
        titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(lineView.snp.bottom).offset(13)
            make.right.equalTo(-15)
        }
        
        addTheImageView()
        let tmpImg = mainView.viewWithTag(201)
        
        let bottomView = UIView()
        bottomView.backgroundColor = TABLE_LINE_COLOR
        mainView.addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(5)
            if tmpImg != nil{
                make.top.equalTo(tmpImg!.snp.bottom).offset(11)
            }else{
                make.top.equalTo(titleLab.snp.bottom).offset(11)

            }
        }
        
        
        
      
        
    }
    func addTheImageView() -> Void {
        
    }
    func setCommentInfo(model : CommentModel) -> Void {
        for i in 0..<3{
            if let bgImg = mainView.viewWithTag(201 + i){
                bgImg.isHidden = true
            }
        }
        if let images = model.blog?.images{
            for i in 0..<images.count{
                let model = images[i]
                if let bgImg = mainView.viewWithTag(201 + i) as? UIImageView{
                    bgImg.isHidden = false
                    if let imgUrl = model.url{
                        bgImg.kf.setImage(with: URL(string: imgUrl))
                    }
                }
                
            }
        }
        titleLab.text = model.blog?.title
        if let time = model.creationTime{
             timeLab.text = time.replacingOccurrences(of: "T", with: " ").components(separatedBy: ".").first
        }
        userLab.text = model.creatorUserDisplayName
        let userImgUrl = model.creatorUserAvatar != nil ?  model.creatorUserAvatar : ""
        userImg.kf.setImage(with: URL(string: userImgUrl!),placeholder: UIImage(named: "login_head_moren"))
        commentLab.text = model.body
//        timeLab.text = model.creationTime
        
    }


    
  

}
